package api.homework.two.two;

import java.util.Random;
import java.util.concurrent.Callable;

/**
 * @author lwj
 * @date 2025-03-11
 * @vesion 1.0.0
 */
public class SellingTicketsCallable implements Callable<String> {
    /**
     * 票数
     */
    private static Integer ticket = 50;

    // 构造方法
    public SellingTicketsCallable() {}
    @Override
    public synchronized String call() throws Exception {
        while(true){
            // 每个人中间买票间隔2秒
            Thread.sleep(1000);

            // 随机卖的票数
            int num = new Random().nextInt(1,10);
            if(ticket > 0){
                int temp = ticket - num;
                if(temp <= 0){
                    num = ticket;
                    ticket -= ticket;
                }else{
                    ticket = temp;
                }
                System.out.println(Thread.currentThread().getName()+"这次卖出了"+num+"张电影票,还剩"+ticket+"张电影票");
            }
            if(ticket == 0){
                break;
            }
        }
        return "票卖完了！";
    }
}
